package com.qiyu.tech.mysql.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

import static com.qiyu.tech.mysql.enums.DatasourceConfigEnum.TEST_DB_02;

/**
 * @Author linhao
 * @Date created in 6:20 下午 2021/1/5
 */
@Configuration
@MapperScan(basePackages = "${db.mybatis.mapperScanner.basePackage}",sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DatasourceConfig2 {


    @Bean(name = "testDB2")
    public DataSource db1DataSource(){

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl(TEST_DB_02.getUrl());
        druidDataSource.setUsername(TEST_DB_02.getUsername());
        druidDataSource.setPassword(TEST_DB_02.getPassword());
        //具体配置
        druidDataSource.setMaxActive(100);
        druidDataSource.setInitialSize(3);
        druidDataSource.setMinIdle(1);
        druidDataSource.setMaxWait(30000);
        //间隔多久才进行一次检测，检测需要关闭的空闲连接，单位是毫秒
        druidDataSource.setTimeBetweenConnectErrorMillis(60000);
        return druidDataSource;
    }


    /**
     * @methodDesc: 功能描述:(test2 sql会话工厂)
     */
    @Bean(name = "test2SqlSessionFactory")
    public SqlSessionFactory test2SqlSessionFactory(@Qualifier("testDB2") DataSource dataSource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        return bean.getObject();
    }
}


